local skel = fk.CreateSkill {
  name = "rmt__zhizhu",
  dynamic_desc = function (self, player, lang)
    local skillName = self.name
    local mark = player:getMark(skillName.."_event")
    if type(mark) == "table" then
      if #mark == 0 then return skillName.."_none" end
      local arg = ""
      for _, v in ipairs(mark) do
        arg = arg..Fk:translate("rmt__zhaoevent_"..v, lang).."，"
      end
      return skillName.."_dyn:".. arg .. ":" .. tostring(#mark)
    end
  end,
}

Fk:loadTranslationTable{
  ["rmt__zhizhu"] = "直诛",
  [":rmt__zhizhu"] = "准备阶段，你可以无次数距离限制地使用一张手牌，手牌数小于X的角色不能响应（X为“直诛”时机数）。若为【杀】且未造成伤害，将当前发动时机移动至“筹刃”。",

  [":rmt__zhizhu_dyn"] = "{1}你可以无次数距离限制地使用一张手牌，手牌数小于X的角色不能响应（X为“直诛”时机数={2}）。若为【杀】且未造成伤害，将当前发动时机移动至“筹刃”。",
  [":rmt__zhizhu_none"] = "无合法时机。你可以无次数距离限制地使用一张手牌，手牌数小于X的角色不能响应（X为“直诛”时机数）。若为【杀】且未造成伤害，将当前发动时机移动至“筹刃”。",
  ["#rmt__zhizhu-use"] = "直诛：你可无次数距离限制地使用一张牌，若为杀且未造成伤害，将当前发动时机移动至“筹刃”",

  ["rmt__zhaoevent_start"] = "准备阶段",
  ["rmt__zhaoevent_damage"] = "每回合首次造成伤害后",
  ["rmt__zhaoevent_cancel"] = "你抵消牌时",
  ["rmt__zhaoevent_death"] = "有角色死亡后",

  ["$rmt__zhizhu1"] = "汝莫欢喜，焉知我不能手刃汝耶？",
  ["$rmt__zhizhu2"] = "杀人者，人恒杀之！",
}

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, self.name.."_event", {"start"})
end)

local zhaoeEventMap = {[fk.Damage] = "damage", [fk.CardEffecting] = "cancel", [fk.Deathed] = "death", [fk.EventPhaseStart] = "start"}

--- 能否触发此时机
---@param player ServerPlayer
local function can_trigger(skill, event, target, player, data)
  if not player:hasSkill(skel.name) then return false end
  local mark = player:getTableMark(skel.name.."_event")
  if not table.contains(mark, zhaoeEventMap[event.class]) then return false end
  if event == fk.EventPhaseStart then
    return player.phase == Player.Start and target == player
  elseif event == fk.Damage then
    if target == player then
      local firstDamage = player.room.logic:getActualDamageEvents(1, function(e) return e.data.from == player end)[1]
      return firstDamage and firstDamage.data == data
    end
  elseif event == fk.CardEffecting then
    -- 抵消牌时，注意，不能使用 CardEffectCancelledOut，fk.CardEffectFinished
    -- data.responseToEvent and data.responseToEvent.card and data.responseToEvent.isCancellOut
    return data.from == player and data.toCard
  elseif event == fk.Deathed then
    return true
  end
  return false
end

---@param player ServerPlayer
local on_cost = function (self, event, target, player, data)
  -- 还是不允许转化用牌比较保险
  local use = player.room:askToUseRealCard(player, {
    pattern = ".|.|.|hand", skill_name = skel.name, prompt = "#rmt__zhizhu-use", skip = true, cancelable = true,
    extra_data = {bypass_distances = true, bypass_times = true}
  })
  if use then
    event:setCostData(self, use)
    return true
  end
end

local on_use = function (self, event, target, player, data)
  local use = event:getCostData(self)
  local room = player.room
  local mark = player:getTableMark("rmt__zhizhu_event")
  local num = #mark
  local tars = {}
  for _, p in ipairs(room.alive_players) do
    if p:getHandcardNum() < num then
      table.insert(tars, p)
    end
  end
  use.disresponsiveList = tars
  room:useCard(use)
  if player.dead then return end
  if use.card.trueName == "slash" and not use.damageDealt then
    local e = zhaoeEventMap[event.class]
    if e then
      table.removeOne(mark, e)
      room:setPlayerMark(player, "rmt__zhizhu_event", mark) -- 不能用 removeTableMark ，会自动把空表值设0
      room:addTableMarkIfNeed(player, "rmt__chouren_event", e)
    end
  end
end

--- 四个时机一模一样

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.CardEffecting, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Deathed, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

return skel
